<?php

namespace App\Http\Controllers\VersionOne;

use App\Http\Controllers\API\AuthUserController;
use App\Http\Requests\UserInfoRequest;
use App\Models\Users\UserInfo;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserInfoController extends AuthUserController
{
    private $userInfo;

    public function __construct(UserInfo $userInfo)
    {
        $this->userInfo = $userInfo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = $this->getAuthUser ();

        return response ()->json ($user);
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserInfoRequest $request)
    {
        //$validated = $request->validated();
        //dd($validated);
        //
        $data = $request->all ();
        foreach ($data as $key => $item) {
            if (is_null ($item)) {
                unset($data[ $key ]);
            }
        }
        $this->userInfo->fill ($data);
        $user = $this->userInfo->save ();
        if ($user) {
            return response ()->json ($this->userInfo);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $user = $this->userInfo->find ($id);

        return response ()->json ($user);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $user = $this->userInfo->find ($id);

        return response ()->json ($user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $model = $this->userInfo->find ($id);
        $data  = $request->all ();
        unset($data['id_card']);
        $model->fill ($data);
        $user = $model->save ();
        if ($user) {
            return response ()->json ($model);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $ret = $this->userInfo->destroy ($id);

        return response ()->json ($ret);
    }
}
